package com.example.redis.config;

import com.example.redis.handler.FastJson2JsonRedisSerializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

//@Configuration
@EnableCaching
public class RedisConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setValueSerializer(new FastJson2JsonRedisSerializer<>(Object.class)); // 使用FastJson2JsonRedisSerializer序列化value值
        template.setKeySerializer(new StringRedisSerializer()); // 使用StringRedisSerializer序列化key值
        template.afterPropertiesSet();
        return template;
    }
}